package code;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class LogEncryption {
	public HashMap keylist=new HashMap();
	
	public void initiateKeylist(){
		keylist= new HashMap();

		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();   
			DocumentBuilder parser = factory.newDocumentBuilder();
			Document d= parser.parse(System.getProperty("user.home") + File.separator + "Atoco" + File.separator + "keylist.xml");
			Element root = d.getDocumentElement();   
			Node n = root.getFirstChild();  
			if (n != null){  

				do {   
					if (n.getNodeType() == Node.ELEMENT_NODE){   
						keylist.put(n.getNodeName(), n.getFirstChild().getNodeValue());
					}   
				}while ((n=n.getNextSibling()) != null );   
			}   

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	public void addtoKeylist(String alias, String type){
		
		keylist.put(alias, type);

		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder;
		try {
			documentBuilder = documentBuilderFactory.newDocumentBuilder();

			Document document = documentBuilder.newDocument();

			Element rootElement = document.createElement("keylist");
			document.appendChild(rootElement);

		
			Set set=keylist.keySet();
			Iterator i= set.iterator();
			String s;
			while(i.hasNext()){
				s=(String)i.next();
				Element em = document.createElement(s);
				em.appendChild(document.createTextNode((String)keylist.get(s)));
				rootElement.appendChild(em);
			}
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(document);
			StreamResult result =  new StreamResult(System.getProperty("user.home") + File.separator + "Atoco" + File.separator + "keylist.xml");
			transformer.transform(source, result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
public void removefromKeylist(String alias){
		
		keylist.remove(alias);

		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder;
		try {
			documentBuilder = documentBuilderFactory.newDocumentBuilder();

			Document document = documentBuilder.newDocument();

			Element rootElement = document.createElement("keylist");
			document.appendChild(rootElement);

		
			Set set=keylist.keySet();
			Iterator i= set.iterator();
			String s;
			while(i.hasNext()){
				s=(String)i.next();
				Element em = document.createElement(s);
				em.appendChild(document.createTextNode((String)keylist.get(s)));
				rootElement.appendChild(em);
			}
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(document);
			StreamResult result =  new StreamResult(System.getProperty("user.home") + File.separator + "Atoco" + File.separator + "keylist.xml");
			transformer.transform(source, result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
